const request = require('supertest');
import api from '@/api';
// 初始化测试数据
const books = {
    Title:"Vue.js 3.0 企业级管理后台开发实战：基于ElementPlus",
    Author:"杨海民",
    Publisher:"电子工业出版社",
    ISBN:"9787121443299",
    PublishDate:"2022-11-01",
    Category:"计算机",
    Stock:"10",
  }
  
  
// 清理测试数据库
beforeEach(async () => {
  await BookModel.deleteMany({});
});

// --- 测试用例集 ---
describe('图书管理模块测试套件', () => {
  // 用例1: 正常新增图书
  test('应成功创建新图书并返回201状态', async () => {
    const res = await api.books.create(books);
    
    expect(res.statusCode).toBe(201);
    expect(res.body).toHaveProperty('_id');
    expect(res.body.title).toBe(books.title);
  });

  // 用例2: 缺失必填字段
  test('标题为空时应返回400错误', async () => {
    const invalidBook = { ...books, title: '' };
    const res = await api.books.create(invalidBook);
    
    expect(res.statusCode).toBe(400);
    expect(res.body.error).toMatch(/标题为必填项/);
  });

  // 用例3: 特殊字符处理
  test('应正确处理含HTML标签的描述字段', async () => {
    const xssBook = {
      ...books,
      description: '<script>alert("xss")</script>测试'
    };
    
    const res = await api.books.create(xssBook);
    
    expect(res.body.description).toBe('测试'); // 假设已做XSS过滤
  });

});